//
//  main.m
//  05 - 练习
//
//  Created by 韩旭 on 16/7/8.
//  Copyright © 2016年 基础. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
    NSArray *arr = @[@"小", @"小赵", @"小李", @"小龙", @"小仓", @"小明"];
    NSMutableArray *stringArr = [NSMutableArray array];
    for (NSInteger i = arr.count - 1 ; i >= 0; i--) {
        [stringArr addObject:arr[i]];
    }
    NSLog(@"%@",stringArr);
    NSDictionary *dic = @{@"name" : @"阿蔚", @"age" : @12, @"sex" : @"变态"};
    NSArray *arrDic = [dic allKeys];
    for (int i = 0; i < arrDic.count; i++) {
        NSString *key = [arrDic objectAtIndex:i];
        id value = [dic objectForKey:key];
        NSLog(@" key = %@ value = %@", key, value);
    }
    //    枚举值（枚举器也是一种遍历方式）
    //   枚举器NSEnumerator依赖于集合类型（数组, 字典, 集合）, 不能单独使用
    NSEnumerator *stringArrayEnumerrator = [stringArr objectEnumerator];
    id value = nil;
    while ((value = [stringArrayEnumerrator nextObject])!= nil) {
        NSLog(@"value = %@", value);
    }
    //    倒叙枚举
    NSEnumerator *rever = [stringArr reverseObjectEnumerator];
    id value1 = nil;
    while (value1 = [rever nextObject]) {
        NSLog(@"%@", value1);
    }
    //    字典的枚举排序(因为字典是无序的 所以没有倒叙);
    //    字典的枚举器里面默认取出来的值， 是value
    NSEnumerator *dicEnumerator = [dic objectEnumerator];
    id valueDic = nil;
    while (valueDic = [dicEnumerator nextObject]) {
        NSArray *keys = [dic allKeysForObject:valueDic];
        NSLog(@" valueDic = %@  %@",valueDic, keys);
    }
//    集合枚举(无序, 也没有倒叙枚举)
    NSSet *set = [NSSet setWithObjects:@"1123", @"324", @"234", @"343", nil];
    NSEnumerator *setEnumerator = [set objectEnumerator];
    id valueSet = nil;
    while (valueSet = [setEnumerator nextObject]) {
        NSLog(@"%@", valueSet);
    }
//  快速枚举 for。。。。in(是对NSEnumerator的分装)
    NSString *see = [NSString string];
    for (NSString *string in stringArr) {
      see = [see stringByAppendingString:string];
    }
//    NSLog(@"%@", see);
//    字典的快速遍历枚举一个字典
    for (NSString *dicValue in dic) {
        NSLog(@"%@", dicValue);
        NSLog(@"%@", dic[dicValue]);
    }
//    快速枚举一个集合
    for (NSString *anyObject in set) {
        NSLog(@"%@", anyObject);
    }
       for (NSString *stringA in [stringArr reverseObjectEnumerator]) {
           NSLog(@"%@", stringA);
    }
//    排序 1:
//    参数1 key ： 排序条件
//    参数2 acending ：YES : (升序) or NO (降序)
    NSMutableArray *arrMutableArray = [NSMutableArray arrayWithObjects:@"1", @"2", @"3", @"4", nil];
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES];
//    数组有排序的方法（sortDescriptor）
//    参数为一个排序条件的数组
     [arrMutableArray sortUsingDescriptors:@[sortDescriptor]];
    NSLog(@"%@", arrMutableArray);
//    排序：可变数组和不可变数组通过sort排序调用的方法不同
//    1.不可变数组, 排序后需要生成新的数组
//    2.可变数组, 排序直接可以在本身之上修改
    
    Person *per = [[Person alloc] init];
    per.name = @"小李子";
    per.age  = 12;
    Person *per1 = [[Person alloc] init];
    per1.name = @"起名好难呐";
    per1.age = 14;
    Person *per2 = [[Person alloc] init];
    per2.name = @"起名不难";
    per2.age = 15;
    NSArray *perArray = @[per, per1, per2];
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:NO];
    perArray = [perArray sortedArrayUsingDescriptors:@[sort]];
    NSLog(@"排序后： %@", perArray);
//      排序2 (字符串)
    NSArray *sortString = @[@"iPhone", @"iPad", @"iTouch", @"iMac", @"Mac Mini"];
    NSLog(@"%@", sortString);
    sortString = [sortString sortedArrayUsingSelector:@selector(compare:)];
    NSLog(@"%@", sortString);
    return 0;
}



